package com.intelligent.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.intelligent.utils.Result;
import com.intelligent.dto.PurchaseReturnAddDTO;
import com.intelligent.dto.PurchaseReturnQueryDTO;
import com.intelligent.service.PurchaseReturnService;
import com.intelligent.vo.PurchaseReturnVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 采购退货管理Controller
 */
@RestController
@RequestMapping("/purchase/return")
public class PurchaseReturnController {

    @Autowired
    private PurchaseReturnService purchaseReturnService;

    /**
     * 分页查询采购退货列表
     */
    @PostMapping("/page")
    public Result<IPage<PurchaseReturnVO>> getPurchaseReturnPage(@RequestBody PurchaseReturnQueryDTO queryDTO) {
        try {
            IPage<PurchaseReturnVO> page = purchaseReturnService.getPurchaseReturnPage(queryDTO);
            return Result.success(page);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("查询采购退货列表失败：" + e.getMessage());
        }
    }

    /**
     * 新增采购退货
     */
    @PostMapping("/add")
    public Result<String> addPurchaseReturn(@RequestBody PurchaseReturnAddDTO addDTO) {
        try {
            boolean success = purchaseReturnService.addPurchaseReturn(addDTO);
            if (success) {
                return Result.success("新增采购退货成功");
            } else {
                return Result.error("新增采购退货失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("新增采购退货失败：" + e.getMessage());
        }
    }

    /**
     * 根据ID查询采购退货详情
     */
    @PostMapping("/detail/{id}")
    public Result<PurchaseReturnVO> getPurchaseReturnDetail(@PathVariable Integer id) {
        try {
            PurchaseReturnVO detail = purchaseReturnService.getPurchaseReturnDetail(id);
            return Result.success(detail);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("查询采购退货详情失败：" + e.getMessage());
        }
    }

    /**
     * 更新采购退货
     */
    @PutMapping("/update")
    public Result<String> updatePurchaseReturn(@RequestBody PurchaseReturnAddDTO updateDTO) {
        try {
            boolean success = purchaseReturnService.updatePurchaseReturn(updateDTO);
            if (success) {
                return Result.success("更新采购退货成功");
            } else {
                return Result.error("更新采购退货失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("更新采购退货失败：" + e.getMessage());
        }
    }

    /**
     * 删除采购退货
     */
    @DeleteMapping("/delete/{id}")
    public Result<String> deletePurchaseReturn(@PathVariable Integer id) {
        try {
            boolean success = purchaseReturnService.removeById(id);
            if (success) {
                return Result.success("删除采购退货成功");
            } else {
                return Result.error("删除采购退货失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("删除采购退货失败：" + e.getMessage());
        }
    }

    /**
     * 导出采购退货数据
     */
    @PostMapping("/export")
    public Result<String> exportPurchaseReturn(@RequestBody PurchaseReturnQueryDTO queryDTO) {
        try {
            // 这里可以扩展导出逻辑
            return Result.success("导出采购退货数据成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("导出采购退货数据失败：" + e.getMessage());
        }
    }
}
